package com.jic.member.controller.power;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.rest.ManagerMerchantApi;
import com.jic.manager.response.MestoreResponse;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.power.MepowerStore;
import com.jic.member.request.power.MepowerStoreRequest;
import com.jic.member.response.power.MepowerStoreResponse;
import com.jic.member.service.power.MepowerStoreService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@ApiOperation(value = "权益店铺关联controller")
@RestController
@RequestMapping("/mePowerStore")
@Slf4j
public class MepowerStoreController {

    @Resource
    MepowerStoreService mePowerStore;
    @Resource
    private ManagerMerchantApi ManagerMerchantApi;
    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取权益店铺所有列表")
    @ResponseBody
    @RequestMapping("/queryPowerStoreList")
    public RestResult<PageResult<MepowerStoreResponse>> queryPowerStoreList(@RequestBody MepowerStoreRequest form) {
        log.info("开始权益店铺列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MepowerStore memberRequest = BeanUtil.copy(form, MepowerStore.class);
        PageResult<MepowerStore> result = mePowerStore.queryPage(memberRequest, page);
        List<MepowerStoreResponse> memberResponseList = result.getRows().stream().map(temp -> {
            MepowerStoreResponse mepowerStoreResponse = new MepowerStoreResponse();
            BeanUtils.copyProperties(temp, mepowerStoreResponse);
            return mepowerStoreResponse;
        }).filter(item -> item != null).collect(Collectors.toList());

        PageResult<MepowerStoreResponse> pageResult = new PageResult<>();
        pageResult.setRows(memberResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }

    @ApiOperation(value = "保存方法")
    @ResponseBody
    @RequestMapping("/savePowerStore")
    public RestResult savePowerStore(@RequestBody MepowerStoreRequest form){
        MepowerStore memberRequest = BeanUtil.copy(form, MepowerStore.class);
        mePowerStore.insert(memberRequest);
        return RestResult.success("1");
    }

    @ApiOperation(value = "删除方法")
    @ResponseBody
    @RequestMapping("/deletePowerStore")
    public RestResult deletePowerStore(@RequestBody MepowerStoreRequest form){
        MepowerStore memberRequest = BeanUtil.copy(form, MepowerStore.class);
        mePowerStore.deleteInfo(memberRequest);
        return RestResult.success("1");
    }

    /*
     * 分页查询店铺列表
     * */
    @ApiOperation(value = "分页查询店铺列表")
    @ResponseBody
    @RequestMapping("/queryStorePage")
    public RestResult<PageResult<MestoreResponse>> queryStorePage(@RequestBody MepowerStoreRequest form) {
        log.info("开始权益店铺列表=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        PageResult<MestoreResponse> pageResult = mePowerStore.queryStorePage(form, page);
        return RestResult.success(pageResult);
    }
    /*
     * 添加全部店铺
     * */
    @ApiOperation(value = "添加全部店铺")
    @ResponseBody
    @RequestMapping("/saveAllStoreInfo")
    public RestResult saveAllStoreInfo(@RequestBody MepowerStoreRequest form){
        MepowerStore memberRequest = BeanUtil.copy(form, MepowerStore.class);
        mePowerStore.saveAllStoreInfo(memberRequest);
        return RestResult.success("1");
    }
}
